package com.web.surf.sample.eshop.database.dao.services;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.sql.Connection;
import com.web.surf.sample.eshop.database.dao.interfaces.IOrderDao;
import com.web.surf.sample.eshop.faces.core.AbstractBean;
import com.web.surf.sample.eshop.faces.managed.beans.AddressBean;
import com.web.surf.sample.eshop.faces.managed.beans.GoodsBean;
import com.web.surf.sample.eshop.faces.managed.beans.OrderBean;

public class OrderDaoImpl extends AbstractDao implements IOrderDao {

	private static final String SQL_INSERT_ADDRESS = "INSERT INTO "
			+ "addresses VALUES(?,?,?,?,?,?,?,?,?,?,?)";
	private static final String SQL_INSERT_ORDER = "INSERT INTO orders VALUES(?,?,?,?,?,?)";
	private static final String SQL_INSERT_BASKET = "INSERT INTO baskets VALUES(?,?,?)";	

	private void insertAddress(AddressBean address) throws SQLException {
		PreparedStatement insertAddress = getConnection().prepareStatement(SQL_INSERT_ADDRESS);
		insertAddress.setString(1, address.getAddressID());
		insertAddress.setString(2, address.getCustomerName());
		insertAddress.setString(3, address.getCountry());		
		insertAddress.setString(4, address.getCity());
		insertAddress.setString(5, address.getStreetName());
		insertAddress.setString(6, address.getStreetKind());
		insertAddress.setString(7, address.getHouse());
		insertAddress.setString(8, address.getBuilding());
		insertAddress.setString(9, address.getFlat());
		insertAddress.setString(10, address.getContactPhone());
		insertAddress.setString(11, address.getZipCode());		
		insertAddress.executeUpdate();
	}

	private void insertBasketGoods(String orderID,
			Map<GoodsBean, Integer> basketGoods) throws SQLException {
		LinkedHashSet<PreparedStatement> inserts = new LinkedHashSet<PreparedStatement>();
		for (GoodsBean goods : basketGoods.keySet()) {
			PreparedStatement insertBasketGoods = getConnection()
					.prepareStatement(SQL_INSERT_BASKET);
			insertBasketGoods.setString(1, orderID);
			insertBasketGoods.setString(2, goods.getGoodsID());
			insertBasketGoods.setInt(3, basketGoods.get(goods));
			inserts.add(insertBasketGoods);
		}
		Iterator<PreparedStatement> i = inserts.iterator();
		while (i.hasNext()) {
			PreparedStatement execute = i.next();
			execute.executeUpdate();
		}
	}

	public void insertOrder(OrderBean order) {
		Connection con = getConnection();
		try {			
			con.setAutoCommit(false);
			insertAddress(order.getAddressBean());
			PreparedStatement insertOrder = getConnection().prepareStatement(
					SQL_INSERT_ORDER);
			insertOrder.setString(1, order.getOrderID());
			insertOrder.setString(2, order.getAddressBean().getAddressID());
			Date sqlOrderDate = new Date(order.getOrderDate().getTime());
			insertOrder.setDate(3, sqlOrderDate);
			insertOrder.setDouble(4, order.getTotalPrice());
			insertOrder.setInt(5, order.getTotalGoodsNumber());
			insertOrder.setDate(6, null);
			insertOrder.executeUpdate();
			insertBasketGoods(order.getOrderID(), order.getBasket());
			con.commit();
		} catch (SQLException e) {
			terminateConnection();
		}	
		  finally {
			  try {
				con.setAutoCommit(true);
			} catch (SQLException e) {					
			}			  
		  }
	}

	public void deleteAllOrders() {
		//TODO
	}
	
	
	protected Map<?, ?> processMapSelect(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	protected int processSelectCount(ResultSet rs) throws SQLException {
		int count = 0;
		if (rs.next()) {
			count = rs.getInt(1);
		}
		return count;
	}

	protected List<AbstractBean> processSelectResult(ResultSet rs)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}